package com.lianzhihui.romeex.ui.me;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.zxing.client.android.Intents;
import com.lianzhihui.romeex.R;
import com.lianzhihui.romeex.adapter.AddressActivityAdapter;
import com.lianzhihui.romeex.bean.SaveAddressInfo;
import com.lianzhihui.romeex.config.AppConstants;
import com.lianzhihui.romeex.ui.base.BaseActivity;
import com.lianzhihui.romeex.utils.GsonUtil;
import com.lianzhihui.romeex.utils.IntentUtils;
import com.lianzhihui.romeex.utils.LogUtil;
import com.lianzhihui.romeex.utils.NoDoubleClickUtils;
import com.lianzhihui.romeex.utils.QrUtils.QRUtils;
import com.lianzhihui.romeex.utils.StatusBarUtil;
import com.lianzhihui.romeex.utils.UserUtils;
import com.lianzhihui.romeex.widget.ZFEmptyRecyclerView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ProjectName: romeex
 * @Package: com.lianzhihui.romeex.ui.me
 * @ClassName: MessageActivity
 * @Description: java类作用描述
 * @Author: zhanghong
 * @CreateDate: 2020/11/2 14:44
 * @UpdateUser: 更新者：
 * @UpdateDate: 2020/11/2 14:44
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class AddressActivity extends BaseActivity implements OnRefreshLoadMoreListener, BaseQuickAdapter.OnItemClickListener {
    @BindView(R.id.include_top_iv_back)
    ImageView includeTopIvBack;
    @BindView(R.id.include_top_lin_newback)
    LinearLayout includeTopLinNewback;
    @BindView(R.id.include_top_tv_title)
    TextView includeTopTvTitle;
    @BindView(R.id.include_top_iv_right_second)
    ImageView includeTopIvRightSecond;
    @BindView(R.id.include_top_tv_right)
    TextView includeTopTvRight;
    @BindView(R.id.include_top_lin_right)
    LinearLayout includeTopLinRight;
    @BindView(R.id.include_top_lin_background)
    LinearLayout includeTopLinBackground;
    @BindView(R.id.app_top_bar)
    LinearLayout appTopBar;
    @BindView(R.id.myRecyclerView)
    ZFEmptyRecyclerView myRecyclerView;
    @BindView(R.id.mySmartRefreshLayout)
    SmartRefreshLayout mySmartRefreshLayout;
    @BindView(R.id.tv_address_btn)
    TextView tvAddressBtn;
    private AddressActivityAdapter messageTransferAdapter;
    private SaveAddressInfo saveAddressInfo;
    private int myRequestCode = 0x1122;

    int isSelect = -1;//0为选择，-1为编辑

    @Override
    protected int getLayoutId() {
        return R.layout.activity_address;
    }

    @Override
    protected void initView() {
        isSelect = getIntent().getIntExtra("isSelect", -1);
        StatusBarUtil.setTransparent(this);
        StatusBarUtil.setStatusBarTextColor(this, false);
        includeTopLinBackground.setBackgroundColor(getResources().getColor(R.color.trade_bg_F4F5F7_color));
        mySmartRefreshLayout.setEnableRefresh(false);
        mySmartRefreshLayout.setEnableLoadMore(false);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        myRecyclerView.setLayoutManager(linearLayoutManager);
        includeTopTvTitle.setText(getResources().getText(R.string.text_address_manager));
    }

    @Override
    protected void initData() {
        String currentSaveAddressList = UserUtils.getCurrentSaveAddressList();
        if (!currentSaveAddressList.isEmpty()) {
            saveAddressInfo = GsonUtil.GsonToBean(currentSaveAddressList, SaveAddressInfo.class);
            messageTransferAdapter = new AddressActivityAdapter(this, saveAddressInfo.getList());
            myRecyclerView.showEmptyView(saveAddressInfo.getList().size(), 1);
        } else {
            messageTransferAdapter = new AddressActivityAdapter(this, new ArrayList<SaveAddressInfo.Address>());
        }
        myRecyclerView.setAdapter(messageTransferAdapter);
        messageTransferAdapter.setOnItemClickListener(this);
    }


    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {

    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {

    }

    @Override
    public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
        if (isSelect == -1) {
            Intent intent = new Intent(this, EditAddressActivity.class);
            Bundle bundle = new Bundle();
            bundle.putSerializable(AppConstants.ITEM_OBJ, saveAddressInfo);
            bundle.putString(AppConstants.ITEM_INDEX, String.valueOf(i));
            intent.putExtras(bundle);
            startActivityForResult(intent, myRequestCode);
        } else {
            //作选择
            Intent intent = new Intent();
            intent.putExtra("bean", (SaveAddressInfo.Address) baseQuickAdapter.getItem(i));
            setResult(RESULT_OK, intent);
            finish();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == myRequestCode && resultCode == RESULT_OK && data != null) {
            Bundle extras = data.getExtras();
            if (extras != null) {
                saveAddressInfo = (SaveAddressInfo) extras.get(AppConstants.ITEM_OBJ);
                if (null != messageTransferAdapter & saveAddressInfo != null) {
                    myRecyclerView.showEmptyView(saveAddressInfo.getList().size(), 1);
                    messageTransferAdapter.setNewData(saveAddressInfo.getList());
                    UserUtils.setCurrentSaveAddressList(GsonUtil.GsonString(saveAddressInfo));
                }
            }
        }

    }


    @OnClick({R.id.tv_address_btn})
    public void OnClick(View v) {
        if (NoDoubleClickUtils.isDoubleClick()) {
            switch (v.getId()) {

                case R.id.tv_address_btn:
                    Intent intent = new Intent(this, AddAddressActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable(AppConstants.ITEM_OBJ, saveAddressInfo);
                    intent.putExtras(bundle);
                    startActivityForResult(intent, myRequestCode);
                    break;

                default:
                    break;
            }
        }
    }

}
